package com.chen.user.system.usersystem.service.impl;


import com.chen.commons.system.commonssystem.util.IdWorker;
import com.chen.order.system.ordersystem.entity.OrderEntity;
import com.chen.user.system.usersystem.entity.UserEntity;
import com.chen.user.system.usersystem.mapper.UserMapper;
import com.chen.user.system.usersystem.service.UserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 用户逻辑层
 * @author :master
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BusinessFeignServiceImpl businessFeignService;

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void addUser(UserEntity userEntity) throws InterruptedException {
        // Thread.sleep(10000);
        // 业务逻辑， 每新增一个用户， 默认给第三方下订单， 付款0.1元
        userEntity.setUserCount(idWorker.nextId()+"");
        userMapper.addUser(userEntity);
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderUser(userEntity.getUserCount());
        orderEntity.setOrderCount(userEntity.getUserCount());
        orderEntity.setOrderMoney("0.1");
        orderEntity.setOrderStatus("1");
        orderEntity.setOrderType("1");
        orderEntity.setOrderName(userEntity.getUserName()+ "下单数据");
        orderEntity.setOrderTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        businessFeignService.placeOrder(orderEntity);
    }

    @Override
    public void updateUser(UserEntity userEntity) {
        userMapper.updateUser(userEntity);
    }

    @Override
    public List<UserEntity> queryUsers() {
        return userMapper.queryUsers();
    }
}
